import numpy as np
from sklearn.neighbors import KNeighborsRegressor
from matplotlib import pyplot as plt

n_dots = 40
X = 5 * np.random.rand(n_dots,1)
y = np.cos(X).ravel()#将数组展平
y += 0.2*np.random.rand(n_dots) - 0.1

#训练模型
k = 5
knn = KNeighborsRegressor(k)
knn.fit(X,y)

#生成足够密集的点进行预测
T = np.linspace(0,5,500)[:,np.newaxis]
y_pred = knn.predict(T)
print("拟合准确性：" + str(knn.score(X,y)))

#画出拟合曲线
plt.figure(figsize=(16,10),dpi=50)
plt.scatter(X,y,c='g',label='data',s=100)#画出训练样本
plt.plot(T,y_pred,c='k',label='prediction',lw=4)#画出拟合曲线
plt.title('KNeighborsRegressor (k = %i )' % k)
plt.show()